import { reqGetChannels, reqGetAllChannels } from '@/api/channels'

export default {
  namespaced: true,
  state: {
    channelList: [],
    allChannelsList: []
  },
  mutations: {
    setChannels (state, channels) {
      state.channelList = channels
    },
    setAllChannels (state, channels) {
      state.allChannelsList = channels
    },
    addNewChannel (state, channel) {
      state.channelList.push(channel)
    },
    deleteChannel (state, index) {
      state.channelList.splice(index, 1)
    }
  },
  actions: {
    // 拿用户自己的channels
    async getChannels ({ commit }) {
      const { data } = await reqGetChannels()
      console.log(data)
      commit('setChannels', data.data.channels)
    },
    // 拿所有的channels，去除掉用户自己的channels，就是其他频道的内容
    async getAllChannels ({ commit }) {
      const { data } = await reqGetAllChannels()
      console.log(data)
      commit('setAllChannels', data.data.channels)
    }
  },
  getters: {
    elseChannels (state) {
      return state.allChannelsList.filter(item => {
        return state.channelList.every(channel => channel.id !== item.id)
      })
    }
  }
}
